package gui;

import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import client.Delivery;
import client.LogIn;
import client.Main;

public class DeliveryGUI extends JFrame{

	static Delivery d;
	
	JPanel panel;
	
	JLabel lblHeader;
	JLabel lblRecName;
	JLabel lblRecAdd;
	JLabel lblRecPhone;
	
	JTextField txtRecName;
	JTextField txtRecAdd;
	JTextField txtRecPhone;
	
	JButton btnBack;
	JButton btnSubmit;
	
	JRadioButton btnrOwnAddress;
	JRadioButton btnrOtherAddress;
	ButtonGroup btnGroup1;
	
	JRadioButton btnrImediateOrder;
	JRadioButton btnrPlaceOrder;
	ButtonGroup btnGroup2;
	
	
	JLabel getLabelRecName()
	{
		if(lblRecName==null)
		{
			lblRecName=new JLabel();
			lblRecName.setText("Recipient Name:");
			lblRecName.setBounds(70, 150, 200, 30);
		}
		return lblRecName;
	}
	
	JLabel getLabelRecAdd()
	{
		if(lblRecAdd==null)
		{
			lblRecAdd=new JLabel();
			lblRecAdd.setText("Recipient Address:");
			lblRecAdd.setBounds(70, 200, 200, 30);
		}
		return lblRecAdd;
	}
	
	JLabel getLabelRecPhone()
	{
		if(lblRecPhone==null)
		{
			lblRecPhone=new JLabel();
			lblRecPhone.setText("Recipient Phone:");
			lblRecPhone.setBounds(70, 250, 200, 30);
		}
		return lblRecPhone;
	}
	
	JTextField getTextRecName()
	{
		if(txtRecName==null)
		{
			txtRecName=new JTextField();
			txtRecName.setBounds(200, 150, 200, 30);
		}
		return txtRecName;
	}
	
	JTextField getTextRecAdd()
	{
		if(txtRecAdd==null)
		{
			txtRecAdd=new JTextField();
			txtRecAdd.setBounds(200, 200, 200, 30);
		}
		return txtRecAdd;
	}
	
	JTextField getTextRecPhone()
	{
		if(txtRecPhone==null)
		{
			txtRecPhone=new JTextField();
			txtRecPhone.setBounds(200, 250, 200, 30);
		}
		return txtRecPhone;
	}
	
	public Delivery getDeliveryDetails()
	{
		return d;
	}
	
	JButton getButtonContinue()
	{
		if(btnSubmit==null)
		{
			btnSubmit=new JButton();
			btnSubmit.setText("Continue");
			btnSubmit.setBounds(200, 300, 100, 40);
		}
		return btnSubmit;
	}
	
	JRadioButton getRadioImdOrder()
	{
		if(btnrImediateOrder==null)
		{
			btnrImediateOrder=new JRadioButton();
			btnrImediateOrder.setText("Immediate Order");
			btnrImediateOrder.setBounds(190, 50, 150, 40);
			btnrImediateOrder.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
			btnrImediateOrder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("Immediate Order()");
					SimpleDateFormat date=new SimpleDateFormat("dd/MM/yyyy");
					SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss");
					Calendar c=Calendar.getInstance();
					String[] dt=new String[4];
					dt[0]=date.format(c.getTime());
					dt[1]=time.format(c.getTime());
					
					c.add(Calendar.HOUR, 3);
					dt[2]= date.format(c.getTime());
					dt[3]=time.format(c.getTime());
					d.setDateTime(dt);
					JOptionPane.showMessageDialog(null, "You will receive your order until: "+dt[2]+" "+dt[3]);
				}
			});

		}
		return btnrImediateOrder;
	}
	
	JRadioButton getRadioPlaceOrder()
	{
		if(btnrPlaceOrder==null)
		{
			btnrPlaceOrder=new JRadioButton();
			btnrPlaceOrder.setText("Place Order");
			btnrPlaceOrder.setBounds(50, 50, 140, 40);
			btnrPlaceOrder.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
			btnrPlaceOrder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("Place Order()");
					new DelivryDateTime();
				}
			});
		}
		return btnrPlaceOrder;
	}
	
	JRadioButton getRadioOtherAdd()
	{
		if(btnrOtherAddress==null)
		{
			btnrOtherAddress=new JRadioButton("Other Address");
			btnrOtherAddress.setBounds(190, 90, 141, 40);
			btnrOtherAddress.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
			btnrOtherAddress.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("otherAdd()");
					txtRecAdd.setText("");
					txtRecName.setText("");
					txtRecPhone.setText("");
				}
			});
		}
		return btnrOtherAddress;
	}
	
	JRadioButton getRadioOwnAdd()
	{
		if(btnrOwnAddress==null)
		{
			btnrOwnAddress=new JRadioButton("Own Address");
			btnrOwnAddress.setBounds(50, 90, 126, 40);
			btnrOwnAddress.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
			btnrOwnAddress.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("ownAdd()");
					txtRecName.setText(d.getRecName());
					txtRecAdd.setText(d.getRecAddress());
					txtRecPhone.setText(d.getRecPhone());
				}
			});
		}
		return btnrOwnAddress;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Delivery");
			lblHeader.setBounds(this.getWidth()/2-300/2, 20, 300, 30);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Cancel");
			btnBack.setBounds(380, 10, 100, 40);
		}
		return btnBack;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
			
			//labels
			panel.add(getLabelHeader());
			panel.add(getLabelRecName());
			panel.add(getLabelRecAdd());
			panel.add(getLabelRecPhone());
			
			//text
			panel.add(getTextRecName());
			panel.add(getTextRecAdd());
			panel.add(getTextRecPhone());
			
			//buttons
			panel.add(getButtonBack());
			panel.add(getButtonContinue());
			
			//radio buttons
			if(btnGroup1==null)
			{
				btnGroup1=new ButtonGroup();
				btnGroup1.add(getRadioOwnAdd());
				btnGroup1.add(getRadioOtherAdd());
			}
			panel.add(getRadioOwnAdd());
			panel.add(getRadioOtherAdd());
			
			if(btnGroup2==null)
			{
				btnGroup2=new ButtonGroup();
				btnGroup2.add(getRadioImdOrder());
				btnGroup2.add(getRadioPlaceOrder());
			}
			panel.add(getRadioImdOrder());
			panel.add(getRadioPlaceOrder());
		}
		return panel;
	}
	
	public DeliveryGUI()
	{
		super();
		init();
	}
	
	void init()
	{
		d=new Delivery();
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 500, 400);
		this.setTitle("Zer Lah - Delivery");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
		d.getRec(LogIn.usr.getID(),LogIn.usr.getName());
	}
}
